﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity.Validation;
using System.Data.Entity.Infrastructure;
using XStudio.XCRF.Data.Entities;
using XStudio.XCRF.Data.Enums;

namespace XStudio.XCRF.Data.Business.Validate
{
    /// <summary>
    /// 医院业务验证类
    /// </summary>
    public class HospitalValidate : ValidateAttribute
    {
        public override IEnumerable<DbValidationError> ValidateAdd(DbEntityEntry entityEntry, XCRFContext context)
        {
            List<DbValidationError> errors = new List<DbValidationError>();
            Hospital hospital = (Hospital)entityEntry.Entity;
            Member currentMember = context.CurrentMember;
            if (currentMember.Type != (byte)MemberTypes.Administrator)
                errors.Add(new DbValidationError(null, "只有管理员可以创建医院"));
            return errors;
        }
        public override IEnumerable<DbValidationError> ValidateEdit(DbEntityEntry entityEntry, XCRFContext context)
        {
            List<DbValidationError> errors = new List<DbValidationError>();
            Hospital hospital = (Hospital)entityEntry.Entity;
            Member currentMember = context.CurrentMember;
            switch ((MemberTypes)currentMember.Type)
            {
                case MemberTypes.Administrator: break;
                case MemberTypes.Owner:
                    if (currentMember.HospitalID != hospital.ID)
                        errors.Add(new DbValidationError("HospitalID", "医院主管只能编辑自己的医院"));
                    break;
                case MemberTypes.Researcher:
                    errors.Add(new DbValidationError(null, "研究者不能修改医院信息"));
                    break;
            }
            return errors;
        }
        public override IEnumerable<DbValidationError> ValidateDelete(DbEntityEntry entityEntry, XCRFContext context)
        {
            List<DbValidationError> errors = new List<DbValidationError>();
            Hospital hospital = (Hospital)entityEntry.Entity;
            Member currentMember = context.CurrentMember;
            switch ((MemberTypes)currentMember.Type)
            {
                case MemberTypes.Administrator: break;
                case MemberTypes.Owner:
                    if (currentMember.HospitalID != hospital.ID)
                        errors.Add(new DbValidationError("HospitalID", "医院主管只能删除自己的医院"));
                    break;
                case MemberTypes.Researcher:
                    errors.Add(new DbValidationError(null, "研究者不能删除医院信息"));
                    break;
            }
            return errors;
        }
    }
}
